package com.coscon.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

public class XssStringJsonDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        if (jsonParser.hasToken(JsonToken.VALUE_STRING)) {
            String value = jsonParser.getValueAsString();
            if (value == null || "".equals(value)) {
                return value;
            }
            return XSSUtils.xssEncodeParameterByJson(value);
        }
        return null;
    }
}
